import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/homework_detail_page/item/state.dart';

import 'action.dart';

Widget buildView(
    HomeworkAnswerState state, Dispatch dispatch, ViewService viewService) {
  var border = OutlineInputBorder(
    borderSide: BorderSide(
      color: Colors.black12,
    ),
  );

  return DefaultTabController(
      length: 2,
      child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text('作业详情'),
            bottom: TabBar(
              isScrollable: false,
              labelColor: Colors.blue,
              indicatorColor: Colors.blue,
              indicatorPadding: EdgeInsets.symmetric(horizontal: 60),
              indicatorWeight: 3,
              tabs: [Tab(text: '学生写作详情'), Tab(text: 'AI批改详情')],
            ),
          ),
          body: TabBarView(
            children: <Widget>[
              SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    Card(
                        color: Colors.white,
                        elevation: 4.0,
                        margin: new EdgeInsets.symmetric(
                            horizontal: 8.0, vertical: 8.0),
                        child: Padding(
                          padding:
                              EdgeInsets.symmetric(horizontal: 16, vertical: 6),
                          child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '教师作文评分',
                                      style: TextStyle(
                                          fontSize: 16,
                                          fontWeight: FontWeight.w500),
                                    ),
                                    Text(
                                      '（默认数据为机评结果，可做点评参考）',
                                      style: TextStyle(
                                          fontSize: 11, color: Colors.grey),
                                    ),
                                  ],
                                ),
                                Divider(height: 1),
                                SizedBox(height: 10),
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '作文分数：',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                    SizedBox(width: 6),
                                    Container(
                                      width: 180,
                                      child: TextField(
                                        keyboardType:
                                            TextInputType.numberWithOptions(),
                                        controller: state.scoreEditController,
                                        focusNode: state.focusNodeScore,
                                        decoration: InputDecoration(
                                          contentPadding: EdgeInsets.all(6),
                                          enabledBorder: border,
                                          focusedBorder: border,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(height: 10),
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '内        容：',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                    SmoothStarRating(
                                        allowHalfRating: false,
                                        onRatingChanged: (v) {
                                          dispatch(
                                              AnswerMarkActionCreator.rating(
                                                  1, v));
                                        },
                                        starCount: 5,
                                        rating: state.rating1,
                                        size: 40.0,
                                        color: Colors.amber,
                                        borderColor: Colors.yellow,
                                        spacing: 0.0)
                                  ],
                                ),
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '表        达：',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                    SmoothStarRating(
                                        allowHalfRating: false,
                                        onRatingChanged: (v) {
                                          dispatch(
                                              AnswerMarkActionCreator.rating(
                                                  2, v));
                                        },
                                        starCount: 5,
                                        rating: state.rating2,
                                        size: 40.0,
                                        color: Colors.amber,
                                        borderColor: Colors.yellow,
                                        spacing: 0.0)
                                  ],
                                ),
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '发        展：',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                    SmoothStarRating(
                                        allowHalfRating: false,
                                        onRatingChanged: (v) {
                                          dispatch(
                                              AnswerMarkActionCreator.rating(
                                                  3, v));
                                        },
                                        starCount: 5,
                                        rating: state.rating3,
                                        size: 40.0,
                                        color: Colors.amber,
                                        borderColor: Colors.yellow,
                                        spacing: 0.0)
                                  ],
                                ),
                                SizedBox(height: 10),
                                Text('作文点评：', style: TextStyle(fontSize: 16)),
                                SizedBox(height: 5),
                                TextField(
                                  maxLines: null,
                                  minLines: 8,
                                  controller: state.contentEditController,
                                  focusNode: state.focusNodeContent,
                                  decoration: InputDecoration(
                                    contentPadding: EdgeInsets.all(8),
                                    hintText: '请输入',
                                    enabledBorder: border,
                                    focusedBorder: border,
                                  ),
                                ),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    FlatButton(
                                      padding: EdgeInsets.symmetric(
                                          horizontal: 16, vertical: 6),
                                      color: Colors.grey,
                                      textColor: Colors.white,
                                      child: Text('重写'),
                                      shape: RoundedRectangleBorder(
                                          side: BorderSide(
                                            color: Colors.white,
                                            width: 0,
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      onPressed: () {
                                        dispatch(AnswerMarkActionCreator
                                            .onReWrite());
                                      },
                                    ),
                                    FlatButton(
                                      padding: EdgeInsets.symmetric(
                                          horizontal: 16, vertical: 6),
                                      color: Colors.blue,
                                      textColor: Colors.white,
                                      child: Text('发布批阅'),
                                      shape: RoundedRectangleBorder(
                                          side: BorderSide(
                                            color: Colors.white,
                                            width: 0,
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      onPressed: () {
                                        dispatch(
                                            AnswerMarkActionCreator.onMark());
                                      },
                                    )
                                  ],
                                ),
                              ]),
                        )),
                  ],
                ),
              ),
              Stack(
                children: <Widget>[
                  Positioned(
                    top: 0,
                    left: 0,
                    right: 0,
                    bottom: 0,
                    child: InAppWebView(
                      initialUrl: GlobalState.baseUrl(
                          '/correct/pub/view/machine/review.mvc?id=${state.id}&ishomework=1'),
                      initialHeaders: {'ticket': GlobalState.ticket},
                      initialOptions: {},
                      onWebViewCreated: (InAppWebViewController controller) {},
                      onLoadStart:
                          (InAppWebViewController controller, String url) {
                        print("started $url");
                      },
                      onProgressChanged:
                          (InAppWebViewController controller, int progress) {
                        print("started $progress");
                        dispatch(AnswerMarkActionCreator.progress(progress));
                      },
                      onLoadStop:
                          (InAppWebViewController controller, String url) {},
                    ),
                  ),
                  Positioned(
                      top: 0,
                      left: 0,
                      right: 0,
                      child: Offstage(
                        offstage: state.webPageProgress == 100,
                        child: SizedBox(
                          height: 2.0,
                          child: LinearProgressIndicator(
                            valueColor:
                                new AlwaysStoppedAnimation<Color>(Colors.green),
//                value: 80,
//                    backgroundColor: Colors.green,
                          ),
                        ),
                      )),
                ],
              ),
            ],
          )));
}
